package com.gurugame.px.util;

/**
 *  __ _ _   _ _ __ _   _ 
 * / _` | | | | '__| | | |
 *| (_| | |_| | |  | |_| |
 * \__, |\__,_|_|   \__,_|
 * |___/
 *
 * from ThreadLocalRandom
 * @author wangsun
 * @version 1.0.0
 * 2015年10月12日 下午7:32:38 created
 */
public class PxRandom{

	private long seed;

	public PxRandom( long seed ){
		this.seed = seed;
	}
	
	public long getSeed(){
		return seed;
	}

	public int nextInt( int bound ){
		if( bound == 1 ){
			return 0 ;
		}
		int r = mix32( nextSeed() );
		int m = bound - 1;
		if( ( bound & m ) == 0 ) // power of two
			r &= m;
		else{ // reject over-represented candidates
			for( int u = r >>> 1 ; u + m - ( r = u % bound ) < 0 ; u = mix32( nextSeed() ) >>> 1 ) ;
		}
		return r;
	}

	private final long nextSeed(){
		return seed += 0x9e3779b97f4a7c15L;
	}

	private final int mix32( long z ){
		z = ( z ^ ( z >>> 33 ) ) * 0xff51afd7ed558ccdL;
		return ( int ) ( ( ( z ^ ( z >>> 33 ) ) * 0xc4ceb9fe1a85ec53L ) >>> 32 );
	}

}
